package com.lumlord.common.log;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lumlord.common.domain.User;
import com.lumlord.common.exception.BusinessException;
import com.lumlord.common.spring.SpringContextUtil;
import com.lumlord.util.JdbcTemplateUtils;

import cn.hutool.core.bean.BeanUtil;


public class LogFactory implements Log{
	private Class<?> clazz;
	private  Logger logger;
	public LogFactory(Class<?> clazz) {
		logger=LoggerFactory.getLogger(clazz);
	}
	
	@Override
	public void debug(String format, Object arg) {
		logger.debug(format, arg);
		
	}

	@Override
	public void debug(String msg) {
		logger.debug(msg);
		
	}

	@Override
	public void debug(String format, Object arg1, Object arg2) {
		logger.debug(format, arg1, arg2);
		
	}

	@Override
	public void debug(String format, Object... arguments) {
		logger.debug(format, format, arguments);
		
	}

	@Override
	public void debug(String msg, Throwable t) {
		logger.debug(msg,t);
		
		
	}

	@Override
	public void info(String msg) {
		 logger.info(msg);
		
	}

	@Override
	public void infoJson(Object msg) {
		if(msg instanceof  String){ 
			logger.info(msg+"");
		}else {
			Gson gson = new GsonBuilder()
					.setDateFormat("yyyy-MM-dd HH:mm:ss")
					.create();
			String jsonBDID = gson.toJson(msg);
			logger.info(jsonBDID);
		}
	}

	@Override
	public void info(String format, Object arg) {
		logger.info(format, arg);
		
	}

	@Override
	public void info(String format, Object arg1, Object arg2) {
		 logger.info(format, arg1, arg2);
		
	}

	@Override
	public void info(String format, Object... arguments) {
		logger.info(format, arguments);
	}
	
	 
	@Override
	public void info(String msg, Throwable t) {
		logger.info(msg, t);
	}

	@Override
	public void values(String str, Object... arguments) {
		 System.out.println(str);
		 for(Object o : arguments) {
			 System.out.println(o);
		 }
	}

	@Override
	public void Index(Object obj) throws BusinessException {
		if(obj instanceof List) {
			List list = (List) obj;
			for(Object o : list) {
				write(o);
			}
		}else {
			write(obj);
		}
	}
	
	private void write(Object obj) throws BusinessException {
		Map<String, Object> map = BeanUtil.beanToMap(obj);
		Gson gson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd HH:mm:ss")
				.create();

		Integer userId = map.get("addUser")==null?0:Integer.parseInt(map.get("addUser")+"");
		 
		User user = SpringContextUtil.getBean(JdbcTemplateUtils.class).load(User.class, userId);
		if(null!=user) {
			map.put("$addUserName", user.getUserName());
		}else {
			map.put("$addUserName", "无用户");
		}
		 
		map.put("classFamily", obj.getClass().getName());
		map.put(ElkFilter.SIGN_INDEX, true);
		String jsonBDID = gson.toJson(map);
		logger.info(jsonBDID);
	}
	 

}
